<?php

quanli_require('framework/phpExecl/PHPExcel.php');

/**
 * 导出到EXCEL
 * @param type $expTitle
 * @param type $expCellName
 * @param type $expTableData
 */
function exportExcel($expTitle, $expCellName, $expTableData , $type=1) {
    $xlsTitle = iconv('utf-8', 'gb2312', $expTitle); //文件名称
    $fileName = $expTitle . date('_YmdHis'); //or $xlsTitle 文件名称可根据自己情况设定
    $cellNum = count($expCellName);
    $dataNum = count($expTableData);
    $objPHPExcel = new \PHPExcel();

    $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');

    for ($i = 0; $i < $cellNum; $i++) {
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i][1]);
    }
    //$objPHPExcel->getActiveSheet(0)->mergeCells('A3:' . $cellName[$cellNum - 1] . '1'); //合并单元格
    if($type == 2){
        // Miscellaneous glyphs, UTF-8
        $cont = 2;
        $cellNum2 = 11;
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < ($cellNum - $cellNum2); $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . $cont,$expTableData[$i][$expCellName[$j][0]], PHPExcel_Cell_DataType::TYPE_STRING);
                $objPHPExcel->getActiveSheet(0)->mergeCells($cellName[$j] . $cont .':' . $cellName[$j] .($cont + $expTableData[$i]['saleDelCount'] - 1) );
                $objPHPExcel->setActiveSheetIndex(0)->getStyle($cellName[$j] . $cont)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            }
            $a = $cont;
            for ($v = 0;$v < $expTableData[$i]['saleDelCount'];$v++){
                $a = $cont + $v;
                for ($k = ($cellNum - $cellNum2);$k < $cellNum;$k++){
                    $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$k] . $a,$expTableData[$i]['saleDel'][$v][$expCellName[$k][0]], PHPExcel_Cell_DataType::TYPE_STRING);
                }
            }
            $a = '';
            $cont = $cont + $expTableData[$i]['saleDelCount'];
        }
    }else{
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($cellName[$j] . ($i + 2),$expTableData[$i][$expCellName[$j][0]], PHPExcel_Cell_DataType::TYPE_STRING);
            }
        }
    }
    ob_end_clean();//清除缓冲区,避免乱码
    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
    header("Content-Disposition:attachment;filename=$fileName.xls"); //attachment新窗口打印inline本窗口打印
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
}

?>